# all imports
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0, os.path.abspath('..\\scripts'))
from fastai.vision.widgets import *
from fastai.interpret import ClassificationInterpretation
from fastbook import *
from helpers import *
import cv2
import torch
import torchvision
from torchvision.models import resnet50, ResNet50_Weights
# get filename from models folder
learner_filename = input()
learner_filename = learner_filename.replace('.pth','')
learner_filename
'resnet_50_2023May08_0614PM'
learner_path = os.path.join(get_models_dir(), learner_filename)
learner_path
'D:\\ENGINEERING\\mezcal\\models\\resnet_50_2023May08_0614PM'
# Load fastai Learner object
data_loader = make_data_loader(get_processed_data_dir(),
batch_size=64)
learn = resnet_learner(data_loader, 50)
learn.load(learner_path)
D:\ENGINEERING\mezcal\venv\lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead. warnings.warn( D:\ENGINEERING\mezcal\venv\lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights. warnings.warn(msg)
<fastai.learner.Learner at 0x21660d39fa0>
from IPython.display import Image
test_data_dir = get_test_data_dir()
for fname in os.listdir(test_data_dir):
image_path = os.path.join(test_data_dir, fname)
image_object = cv2.imread(image_path)
prediction,_,probs = learn.predict(image_object)
print(f'{max(100*probs):.2f}% {prediction} - {image_path}')
image = Image(filename=image_path, width=224)
display(image)
58.46% chino_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA A_1.jpg
67.26% chino_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA A_2.jpg
94.16% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA B.jpg
65.13% tobala_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA C.jpg
97.26% tobala_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA C_2.jpg
100.00% tobala_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA C_3.jpg
99.98% tobala_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA C_4.jpg
99.75% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA D.jpg
97.10% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA D_2.jpg
92.11% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA E.jpg
99.33% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA F.jpg
99.99% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA G.jpg
99.92% espadin_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRA H.jpg
44.29% chino_impuro - D:\ENGINEERING\mezcal\data\test\MUESTRAA.jpg
interp = ClassificationInterpretation.from_learner(learn)
cm = interp.confusion_matrix()
interp.plot_confusion_matrix()
interp.plot_top_losses(9)